﻿using System;

namespace Microsoft.CSA.FeedSync.FeedSyncLibrarySL
{
    public class RSSFeed : Microsoft.CSA.FeedSync.FeedSyncLibrarySL.FeedSyncFeed<Microsoft.CSA.FeedSync.FeedSyncLibrarySL.RSSFeedSyncItem>
    {
        private string m_Title;
        private string m_Description;
        private string m_Link;

        internal RSSFeed()
        {
        }

        public RSSFeed(string i_Title, string i_Description, string i_Link)
        {
            if (System.String.IsNullOrEmpty(i_Title))
                throw new System.ArgumentException("Invalid value for i_Title!");

            string FeedContents = System.String.Format
                (
                Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RSS_FEED_TEMPLATE,
                i_Title,
                System.String.IsNullOrEmpty(i_Description) ? "" : i_Description,
                System.String.IsNullOrEmpty(i_Link) ? "" : i_Link
                );

            System.Xml.Linq.XDocument XDocument = System.Xml.Linq.XDocument.Parse(FeedContents);

            System.Xml.XmlQualifiedName XmlQualifiedName = new System.Xml.XmlQualifiedName(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RSS_CHANNEL_ELEMENT);
            this.Initialize
                (
                XDocument,
                XmlQualifiedName
                );
        }

        public RSSFeed(System.Xml.Linq.XDocument i_XDocument)
        {
            if (i_XDocument == null)
                throw new System.ArgumentException("i_XDocument must be non-null!");

            System.Xml.XmlQualifiedName ItemContainerXmlQualifiedName = new System.Xml.XmlQualifiedName(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RSS_CHANNEL_ELEMENT);
            this.Initialize
                (
                i_XDocument,
                ItemContainerXmlQualifiedName
                );
        }

        private new void Initialize(System.Xml.Linq.XDocument i_XDocument, System.Xml.XmlQualifiedName i_ItemContainerXmlQualifiedName)
        {
            base.Initialize
                (
                i_XDocument,
                i_ItemContainerXmlQualifiedName
                );

            //  Get title
            System.Xml.Linq.XElement TitleXElement = m_ItemContainerXElement.Element(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RSS_TITLE_ELEMENT_NAME);
            if (TitleXElement != null)
                m_Title = TitleXElement.Value;

            //  Get description
            System.Xml.Linq.XElement DescriptionXmlNode = m_ItemContainerXElement.Element(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RSS_DESCRIPTION_ELEMENT_NAME);
            if (DescriptionXmlNode != null)
                m_Description = DescriptionXmlNode.Value;

            //  Get link
            System.Xml.Linq.XElement LinkXmlNode = m_ItemContainerXElement.Element(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RSS_LINK_ELEMENT_NAME);
            if (LinkXmlNode != null)
                m_Link = LinkXmlNode.Value;
        }

        public string Title
        {
            get
            {
                return m_Title;
            }
        }

        public string Description
        {
            get
            {
                return m_Description;
            }
        }

        public string Link
        {
            get
            {
                return m_Link;
            }
        }

        public System.Xml.Linq.XElement ChannelXElement
        {
            get
            {
                return m_ItemContainerXElement;
            }
        }

        protected override void PopulateItems()
        {
            //  Iterate items
            var FeedSyncItemXElements = m_ItemContainerXElement.Elements(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RSS_ITEM_ELEMENT_NAME);

            foreach (System.Xml.Linq.XElement FeedSyncItemXElement in FeedSyncItemXElements)
            {
                Microsoft.CSA.FeedSync.FeedSyncLibrarySL.RSSFeedSyncItem RSSFeedSyncItem = new RSSFeedSyncItem(FeedSyncItemXElement);

                m_FeedSyncItemDictionary[RSSFeedSyncItem.Sync.ID] = RSSFeedSyncItem;
            }
        }
    }
}
